package com.situ.mall.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.situ.mall.common.entity.*;
import com.situ.mall.common.exception.MyException;
import com.situ.mall.common.mapper.*;

import com.situ.mall.common.service.IOrderInfoService;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.util.XueHuaUtil;

import com.situ.mall.common.vo.OrderVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lwj
 * @since 2023-09-19
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private MemberAddressMapper memberAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderInfo saveOrder(String ids, Integer addressId) {
        Member m = (Member) RequestUtil.getCurrent();

        //创建一个OrderInfo 对象 保存数据库
        OrderInfo oi = new OrderInfo();
        Long l = XueHuaUtil.OrderNumberGenerator.generateOrderNumber();//生成订单，使用雪花算法
        String str = "" + l;
        oi.setCode(str);
        oi.setCreateTime(LocalDateTime.now());
        oi.setMemberId(m.getId());
        oi.setStatus("0");
        BigDecimal sum = new BigDecimal("0");
        for (String cid : ids.split(",")) {
            Cart c = cartMapper.selectByPId(Integer.parseInt(cid), m.getId());
            if (c==null){
                throw new MyException("购物车中没有该商品");
            }
            //得到购物车的一列找出商品id
            Product p = productMapper.selectById(c.getProductId());//根据商品id查到对应的具体商品
            sum = sum.add(p.getPrice().multiply(new BigDecimal(c.getAmount())));//计算总金额
        }


        oi.setPrice(sum);
        baseMapper.insert(oi);
        //创建多个OrderDetail 保存进数据库
        for (String cid : ids.split(",")) {
            Cart c = cartMapper.selectByPId(Integer.parseInt(cid), m.getId());//得到购物车的一列找出商品id
            Product p = productMapper.selectById(c.getProductId());//根据商品id查到对应的具体商品
            OrderDetail od = new OrderDetail();
            od.setProductId(Integer.parseInt(cid));
            od.setOrderId(oi.getId());
            od.setAmount(c.getAmount());
            od.setOldPrice(p.getPrice());//根据vo返回的具体单个商品得到商品价格
            orderDetailMapper.insert(od);
        }

        //创建OrderAddress 保存进数据库
        MemberAddress ma = memberAddressMapper.selectById(addressId);//根据id查到会员选择的收货地址id

        OrderAddress oa = new OrderAddress();
        BeanUtils.copyProperties(ma, oa);//注意导包问题
        //把会员所选择的收货地址信息拷贝到订单地址数据库中
        oa.setOrderId(oi.getId());
        oa.setId(null);
        orderAddressMapper.insert(oa);
        //把购物车中的数据删除
        for (String cid : ids.split(",")) {
            Cart c = cartMapper.selectByPId(Integer.parseInt(cid), m.getId());
            cartMapper.deleteById(c.getId());
        }

        //往消息队列mq中放一个消息：放上订单的id
        //写一个工具类，具有延迟发消息的功能
//        Activemq.delaySend(new ActiveMQQueue("aaa"),oi.getId(),15*60*1000L);//往哪个队列放消息，放什么消息，延迟时间是多少

        return oi;
    }


    @Override
    public TableVO list(Integer limit, Integer page, String status, Integer mId) {
        PageHelper.startPage(page,limit);
//        List<OrderInfo> list = baseMapper.list(status, mId);
//        PageInfo<OrderInfo> pageInfo = new PageInfo<>(list);
//        TableVO tableVO = new TableVO();
//        tableVO.setData(pageInfo.getList());
//        tableVO.setCount(pageInfo.getTotal());
        return TableVO.data(baseMapper.list(status, mId));
    }

    @Override
    public void myDelete(Integer id) {
        baseMapper.myDelete(id);
    }

    @Override
    public TableVO select(Integer page, Integer limit,  String code, String status) {
        PageHelper.startPage(page,limit);

       return TableVO.data(baseMapper.selectOrder(status,code));
    }

    @Override
    public void sendGoods(Integer id) {
        baseMapper.sendGoods(id);
    }

    @Override
    public void confirm(Integer id) {
        baseMapper.confirm(id);
    }

    @Override
    public TableVO list2(Integer id) {
        TableVO vo=new TableVO();
        List<OrderVO> list=orderInfoMapper.list2(id);
        vo.setData(list);
        return vo;
    }

    @Override
    public void paySuccess(String code) {
        baseMapper.paySuccess(code);
    }

    //15分钟未支付 修改订单状态为已取消 没写已取消的值，用已完成代替了
    @Override
    public void updateOrderStatus(Integer id) {
        baseMapper.updateOrderStatus(id);
    }


}
